package com.csi.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.csi.annotation.Controller;
import com.csi.annotation.RequestMapping;
import com.csi.domain.Employ;
import com.csi.service.CommentService;
import com.csi.service.EmployService;
import com.csi.service.impl.CommentServiceImpl;
import com.csi.service.impl.EmployServiceImpl;
import com.csi.utils.DateUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Time;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class EmployController {

    //查看所有员工信息
    @RequestMapping("/employList.do")
    public void employ(HttpServletRequest req, HttpServletResponse resp){
        resp.setContentType("application/json;charset=utf-8");
        EmployService employService =new EmployServiceImpl();
        List<Employ> employs =employService.list();
        String s = JSON.toJSONString(employs);
        try {
            resp.getWriter().write(s);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    //增加员工
    @RequestMapping("/employAdd.do")
    public void add(HttpServletRequest req,HttpServletResponse resp){
        DateUtils dateUtils =new DateUtils();
        resp.setContentType("application/json;charset=utf-8");
        String employNumber = req.getParameter("employNumber");
        String employUsername = req.getParameter("employUsername");
        String wages = req.getParameter("wages");
        String upDates = req.getParameter("upDates");
        String downDate = req.getParameter("downDate");
        String employGrade = req.getParameter("employGrade");
        Employ employ =new Employ();
        employ.setUpDates(DateUtils.StringToTime(upDates));
        employ.setDownDate(DateUtils.StringToTime(downDate));
        employ.setEmployUsername(employUsername);
        employ.setEmployNumber(Integer.parseInt(employNumber));
        employ.setWages(Integer.parseInt(wages));
        System.out.println(employGrade);
        if("0".equals(employGrade)){
            employ.setEmployGrade("管理员");
        }else {
            employ.setEmployGrade("员工");
        }
        EmployService employService =new EmployServiceImpl();
        int i = employService.add(employ);
        String s = JSON.toJSONString(i);

        if(i!=0){
            try {
                resp.getWriter().write(s);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

    //删除员工
    @RequestMapping("/employDeleteList.do")
    public void deleteList(HttpServletRequest req,HttpServletResponse resp){
        resp.setContentType("application/json;charset=utf-8");
        String parseJSON = req.getParameter("parseJSON");
        EmployService employService =new EmployServiceImpl();
        List<Employ> list =JSONArray.parseArray(parseJSON,Employ.class);
        int count =0;
        for (Employ employ:list) {
            int delete = employService.delete(employ.getEmployId());
            count = delete==1?count:count+1;
        }
        String s = JSON.toJSONString(count);
        try {
            resp.getWriter().write(s);
        } catch (IOException e) {
            e.printStackTrace();
        }




    }



    @RequestMapping("/employUpdate.do")
    public void updateEmploy(HttpServletRequest req ,HttpServletResponse resp){
        resp.setContentType("application/json;charset=utf-8");
        String parseJSON = req.getParameter("parseJSON");
        Employ employ = JSON.parseObject(parseJSON,Employ.class);
//        Time upDates = employ.getUpDates();
        EmployService employService =new EmployServiceImpl();
//        String employNumber = req.getParameter("employNumber");
//        String employUsername = req.getParameter("employUsername");
//        String wages = req.getParameter("wages");
//        String upDates = req.getParameter("upDates");
//        String downDate = req.getParameter("downDate");
//        String employGrade = req.getParameter("employGrade");
//        Employ employ =new Employ();
//        employ.setUpDates(DateUtils.StringToTime(upDates));
//        employ.setDownDate(DateUtils.StringToTime(downDate));
//        employ.setEmployUsername(employUsername);
//        employ.setEmployNumber(Integer.parseInt(employNumber));
//        employ.setWages(Integer.parseInt(wages));
//        employ.setEmployGrade(Integer.parseInt(employGrade));
//        EmployService employService =new EmployServiceImpl();
        int i = employService.update(employ);
        String s = JSON.toJSONString(i);
        try {
            resp.getWriter().write(s);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }


    //删除员工
    @RequestMapping("/employDelete.do")
    public void deleteById(HttpServletRequest req,HttpServletResponse resp){
        resp.setContentType("application/json;charset=utf-8");
        String parseJSON = req.getParameter("parseJSON");
        EmployService employService =new EmployServiceImpl();
        Employ employ = JSON.parseObject(parseJSON,Employ.class);

        int delete = employService.delete(employ.getEmployId());

        String s = JSON.toJSONString(delete);
        try {
            resp.getWriter().write(s);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }


}
